import java.io.*;
import java.util.*;

public class StudentInfoProcessor {
    
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入选项编号：");
        int option = scanner.nextInt();
        scanner.nextLine(); 
        switch (option) {
            case 0:
                readAllContent();
                break;
            case 1:
                extractNamesAndScores();
                break;
            case 2:
                printHighestScoreStudent();
                break;
            case 3:
                calculateAverageScores();
                break;
            case 4:
                queryScore(scanner);
                break;
            case 5:
                modifyScore(scanner);
                break;
            default:
                System.out.println("无效的选项编号！");
                break;
        }

        scanner.close();
    }

    private static void readAllContent() throws IOException {
        File file = new File("data.txt");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
        reader.close();
    }

    // 读取data.txt，输出学生的姓名和分数到文件studs.txt
    private static void extractNamesAndScores() throws IOException {
        File inputFile = new File("data.txt");
        File outputFile = new File("studs.txt");
        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));

        String line;
        while ((line = reader.readLine()) != null) {
            String[] parts = line.split(":");
            String name = parts[0];
            String[] scoreParts = parts[1].split(",");
            String score = scoreParts[1];
            writer.write(name + ":" + score);
            writer.newLine();
        }

        reader.close();
        writer.close();
    }

    // 选出分数最高的学生，打印输出学生的姓名和分数
    private static void printHighestScoreStudent() throws IOException {
        File file = new File("data.txt");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        String highestScoreStudent = null;
        int highestScore = 0;

        while ((line = reader.readLine()) != null) {
            String[] parts = line.split(":");
            String name = parts[0];
            String[] scoreParts = parts[1].split(",");
            int score = Integer.parseInt(scoreParts[1]);
            if (score > highestScore) {
                highestScore = score;
                highestScoreStudent = name + ":" + score;
            }
        }

        reader.close();
        System.out.println(highestScoreStudent);
    }

    // 计算每个班级的平均分，打印输出班级和平均分
    private static void calculateAverageScores() throws IOException {
        File file = new File("data.txt");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        Map<String, Integer> classScores = new HashMap<>();
        Map<String, Integer> classCount = new HashMap<>();

        while ((line = reader.readLine()) != null) {
            String[] parts = line.split(":");
            String className = parts[1].split(",")[0];
            int score = Integer.parseInt(parts[1].split(",")[1]);
            classScores.put(className, classScores.getOrDefault(className, 0) + score);
            classCount.put(className, classCount.getOrDefault(className, 0) + 1);
        }

        reader.close();

        for (String className : classScores.keySet()) {
            double averageScore = (double) classScores.get(className) / classCount.get(className);
            System.out.printf("%s:%.2f\n", className, averageScore);
        }
    }

    // 查询成绩
    private static void queryScore(Scanner scanner) throws IOException {
        System.out.println("请输入学生姓名：");
        String name = scanner.nextLine();

        File file = new File("data.txt");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        boolean found = false;

        while ((line = reader.readLine()) != null) {
            String[] parts = line.split(":");
            if (parts[0].equals(name)) {
                found = true;
                String className = parts[1].split(",")[0];
                int score = Integer.parseInt(parts[1].split(",")[1]);
                System.out.println("系名: " + className + ", 成绩: " + score);
                break;
            }
        }

        if (!found) {
            System.out.println("未找到该学生！");
        }

        reader.close();
    }

    // 修改成绩
    private static void modifyScore(Scanner scanner) throws IOException {
        System.out.println("请输入学生姓名：");
        String name = scanner.nextLine();
        System.out.println("请输入新的分数：");
        int newScore = scanner.nextInt();
        scanner.nextLine(); // 消耗掉换行符

        File inputFile = new File("data.txt");
        File outputFile = new File("temp.txt");
        BufferedReader reader = new BufferedReader(new FileReader(inputFile));
        BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));

        String line;
        boolean modified = false;

        while ((line = reader.readLine()) != null) {
            String[] parts = line.split(":");
            if (parts[0].equals(name)) {
                modified = true;
                writer.write(parts[0] + ":" + parts[1].split(",")[0] + "," + newScore);
            } else {
                writer.write(line);
            }
            writer.newLine();
        }

        reader.close();
        writer.close();

        if (modified) {
            inputFile.delete();
            outputFile.renameTo(inputFile);
        } else {
            outputFile.delete();
            System.out.println("未找到该学生！");
        }
    }
}
